package org.neuedu.chapter04_0617;

public class MyDemo01 {
    public static void main(String[] args) {
        // 求对角线和
        // [0][0] [0][2] [1][1] [2][0] [2][2]
        int sum = 0;
        int[][] arr = {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9},
        };

        // 5只加1次
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (i == j || i + j == 2) {
                    sum = sum + arr[i][j];
                }
            }
        }

        // 5加2次
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (i == j) {
                    sum = sum + arr[i][j];
                }
                if (i + j == 2) {
                    sum = sum + arr[i][j];
                }
            }
        }
        System.out.println(sum);
    }
}
